package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.data.Page;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.pojo.TbGoods;
import com.dongdong.pojo.TbGoodsExample;
import com.dongdong.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Override
    public Page<TbGoods> selectList(String goodsName, Integer pageNum, Integer pageSize) {
        String name = goodsName;
        TbGoodsExample te = new TbGoodsExample();
        if (goodsName == null){
            name = "";
            te.createCriteria().andGoodsNameLike(name);
        }else {
            te.createCriteria().andGoodsNameLike(name);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoods> goodsList = tbGoodsMapper.selectByExample(te);
        PageInfo<TbGoods> info = new PageInfo<>(goodsList);
        return new Page<>(info.getPageNum(),info.getPageSize(),info.getTotal(),info.getPages(),info.getList());
    }

    @Override
    public int deleteByIds(Long[] ids) {
        TbGoodsExample te = new TbGoodsExample();
        te.createCriteria().andIdIn(Arrays.asList(ids));
        return tbGoodsMapper.deleteByExample(te);
    }

    @Override
    public int update(Long[] ids) {
        TbGoodsExample te = new TbGoodsExample();
        te.createCriteria().andIdIn(Arrays.asList(ids));
        TbGoods tbGoods = new TbGoods();
        tbGoods.setAuditStatus("2");
        return tbGoodsMapper.updateByExampleSelective(tbGoods,te);
    }
    @Override
    public int statusOne(Long[] ids) {
        TbGoodsExample te = new TbGoodsExample();
        te.createCriteria().andIdIn(Arrays.asList(ids));
        TbGoods tbGoods = new TbGoods();
        tbGoods.setAuditStatus("1");
        int i = tbGoodsMapper.updateByExampleSelective(tbGoods, te);
        return i;
    }

    @Override
    public TbGoods selectGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }
}
